package com.settlement.system.controller;


import com.settlement.system.common.util.SecurityUtils;
import com.settlement.system.common.util.common.FileByteUtils;
import com.settlement.system.common.util.common.SystemUserUtil;
import com.settlement.system.mapper.security.userdetails.SysUserDetails;
import com.settlement.system.model.dto.FileInfo;
import com.settlement.system.model.entity.common.FileByte;
import com.settlement.system.model.entity.common.SystemUser;
import com.settlement.system.model.entity.common.TblAcsUserFile;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.ITblAcsUserFileService;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author
 */
@RestController
@RequestMapping(value = "file")
@Slf4j
public class FileUpLoadController {

    /*@Reference(version = "${dubbo.dl.api.version}")
    private FileUploadApi fileUploadApi;*/

    @Resource
    private ITblAcsUserFileService iTblAcsUserFileService;


    @ApiOperation("上传文件")
    @PostMapping("upload")
    public ObjectDataResponse upload(MultipartFile file, HttpServletRequest request, @RequestParam("platform") String platform) {
        try {
            List<FileByte> fileByteList = FileByteUtils.getFileByte(file);
            List<TblAcsUserFile> userFiles = upLoadFileToService(fileByteList, file, platform);
            iTblAcsUserFileService.batchAdd(userFiles);
            return new ObjectDataResponse(200, "导入成功");
        } catch (Exception e) {
            log.error("上传文件异常", e);
            return new ObjectDataResponse(500, "新增失败，请联系管理员！");
        }
    }

    @ApiOperation("更新文件")
    @PostMapping("update")
    public ObjectDataResponse updateFile(MultipartFile file, HttpServletRequest request,
                                         @RequestParam("id") String id) {
        final String zipExt = ".zip";
        try {
            SysUserDetails user = SecurityUtils.getUser();;
            TblAcsUserFile userFile = iTblAcsUserFileService.getUserFileById(id);
            if (userFile == null) {
                return new ObjectDataResponse(500, "原始文件不存在！");
            }
            if (file.getOriginalFilename().endsWith(zipExt)) {
                return new ObjectDataResponse(500, "文件不能是压缩文件！");
            }
            List<FileByte> fileByteList = FileByteUtils.getFileByte(file);
            if (CollectionUtils.isEmpty(fileByteList)) {
                /*fileUploadApi.delete(userFile.getFilePath());
                fileUploadApi.upload(fileByteList.get(0).getContent(),userFile.getFilePath());*/
                userFile.setStatus(1);
                userFile.setExMessage("");
                userFile.setUpdateTime(new Date());
                userFile.setUpdateUser(user.getUsername());
                iTblAcsUserFileService.update(userFile);
            }
            return new ObjectDataResponse(200, "更新成功");
        } catch (Exception e) {
            log.error("更新文件异常", e);
            return new ObjectDataResponse(500, "更新失败，请联系管理员！");
        }
    }

    /**
     * 上传文件到服务器
     *
     * @param fileBytes
     * @return
     */
    private List<TblAcsUserFile> upLoadFileToService(List<FileByte> fileBytes,
                                                     MultipartFile multipartFile,
                                                     String platform) throws Exception {

        SysUserDetails user = SecurityUtils.getUser();;
        List<TblAcsUserFile> userFiles = new ArrayList<>();
        for (FileByte file : fileBytes) {
            // FileInfo uploadFile = fileUploadApi.upload(file.getContent(), file.getFilePath());
            FileInfo uploadFile = null;
            TblAcsUserFile userFile = new TblAcsUserFile();
            userFile.setFilePath(uploadFile.getPath());
            userFile.setFileName(uploadFile.getFileName());
            userFile.setId(UUID.randomUUID().toString().replace("-", ""));
            userFile.setCreateTime(new Date());
            userFile.setCreateUser(user.getUsername());
            userFile.setUpdateUser(user.getUsername());
            userFile.setUpdateTime(new Date());
            userFile.setOriginalFileName(multipartFile.getOriginalFilename());
            userFile.setPlatform(platform);
            userFile.setExMessage(null);
            userFile.setStatus(1);
            userFiles.add(userFile);
        }
        return userFiles;
    }
}
